package com.code.qrcode.controller;


import com.code.qrcode.bean.dto.ResDto;
import com.code.qrcode.bean.model.User;
import com.code.qrcode.utils.Base64Util;
import com.code.qrcode.utils.QrCodeUtil;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/api/v1/qr-code")
@AllArgsConstructor
@Validated
public class QrCodeGerateController {


    @RequestMapping("/qrcode")
    public ResDto generateQrCode() throws UnsupportedEncodingException {
        User user = new User();
        String code = "www.baidu.com";
        user.setCode(code);
        ByteArrayOutputStream out = null;
        out =  new ByteArrayOutputStream();
        QrCodeUtil.createQrCode(out, code, 500, 500,"png", "");
        String qrCode = new String(Base64Util.baseEncode(out.toByteArray()), "UTF-8");
        qrCode = "data:image/jpg;base64," + qrCode;
        ResDto resDto = new ResDto();
        resDto.setCode(qrCode);
        return resDto;
    }
}
